

## FIGURE 5

# load, combine data
library(rio)
library(tidyverse)

# make better axis labels
library(scales)




# LOAD DATA
dpi_complete <- import("dpi-mexico.csv")

dpi <- import("DPI2020.dta") %>%
  select(countryname,year,gov1me,gov1seat,gov2me,gov2seat,opp1me,opp1seat,
         opp2me,opp2seat,opp3me,opp3seat,totalseats) %>%
  filter(countryname=="Mexico") %>%  
  mutate_all(~na_if(., '-999')) %>%
  select(c(-countryname))

first_party <- dpi %>%
  select(year,gov1me,gov1seat,totalseats) %>%
  rename(party = gov1me,
         seats = gov1seat)
second_party <- dpi %>%
  select(year,opp1me,opp1seat,totalseats) %>%
  rename(party = opp1me,
         seats = opp1seat)
third_party <- dpi %>%
  select(year,opp2me,opp2seat,totalseats) %>%
  rename(party = opp2me,
         seats = opp2seat)
fourth_party <- dpi %>%
  select(year,opp3me,opp3seat,totalseats) %>%
  rename(party = opp3me,
         seats = opp3seat)
dpi2 <- rbind(first_party,second_party,third_party,fourth_party) %>%
  filter(party=="PRI" | party=="PAN" | party=="PRD" | party=="Morena") %>%
  mutate(seatshare = seats/totalseats,
         party = as.factor(party)) %>%
  unique() 


dpi2$seatshare <- as.numeric(dpi2$seatshare)
dpi2$year <- as.numeric(dpi2$year)

dpi2$party <- relevel(dpi2$party, ref="PRD")
dpi2$party <- relevel(dpi2$party, ref="PAN")
dpi2$party <- relevel(dpi2$party, ref="PRI")

dpi_complete$party <- as.factor(dpi_complete$party)
dpi_complete$party <- relevel(dpi_complete$party, ref="PRD")
dpi_complete$party <- relevel(dpi_complete$party, ref="PAN")
dpi_complete$party <- relevel(dpi_complete$party, ref="PRI")


ggplot(dpi_complete, aes(x=year,y=vote)) + geom_line(aes(linetype=party)) + theme_minimal() +
  labs(x="Year", y="Vote Share in the Chamber of Deputies", linetype = "Party") + 
  geom_vline(xintercept = 2000.75, color="#21908CFF") +
  geom_vline(xintercept = 2001, color="#21908CFF") +
  geom_vline(xintercept = 2006, color="#21908CFF") +
  geom_vline(xintercept = 2007.25, color="#21908CFF") +
  geom_vline(xintercept = 2013.75, color="#21908CFF") +
  geom_vline(xintercept = 2014.5, color="#21908CFF") +
  theme(legend.position="bottom") +
  scale_y_continuous(labels = label_percent(scale = 1))

# ggsave("figure5.pdf", height = 5, width = 7.5)
